package view.backend.customer;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.xml.parsers.ParserConfigurationException;
import model.CustomerSpecified;
import org.xml.sax.SAXException;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminCustomerTelephone
{
    private JPanel adminTelephonePanel;
    private AdminCustomer adminCustomer;
    private JTextField tFieldNumber;

    public AdminCustomerTelephone(AdminCustomer view)
    {
        this.adminCustomer = view;
        
        adminTelephonePanel = new JPanel(null);
        adminTelephonePanel.setBounds(0, 0, 300, 80);
        adminTelephonePanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Telefon", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        adminTelephonePanel.setOpaque(false);
        
        JLabel lblName = new JLabel("Nummer");
        lblName.setBounds(10, 25, 150, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldNumber = new JTextField();
        tFieldNumber.setBounds(10, 45, 250, 25);
        tFieldNumber.setBorder(DesignLayout.T_FIELD_RED);
        tFieldNumber.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldNumber.setFont(DesignLayout.STANDARD);
        tFieldNumber.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldNumber.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                JTextField tfield = (JTextField) e.getComponent();
                if (tfield.getText().length() == 8)
                {
                    adminCustomer.getTelephoneModel().setNumber(tfield.getText());
                    doLookUp(tfield.getText());
                }
                else if (tfield.getText().length() > 8)
                {
                    tfield.setText(""+tfield.getText().subSequence(0, 8));
                }
                else
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(false);
                    adminCustomer.setExistingCustomerPanelVisible(false);
                    adminCustomer.updateNewCustomerPanel();
                    adminCustomer.setNewCustomerPanelVisible(false);
                }
            }
        });
        
        adminTelephonePanel.add(lblName);
        adminTelephonePanel.add(tFieldNumber);
        
        adminTelephonePanel.setVisible(false);
    }
    
    public void setNumber(String number)
    {
        tFieldNumber.setText(number);
        doLookUp(number);
    }
    
    private void doLookUp(String number)
    {
        try
        {
            CustomerSpecified[] customerSpecified = model.handlers.CustomerHandler.getSpecifiedCustomersByNumber(number);
            if (customerSpecified.length > 0)
            {
                adminCustomer.buildFoundPanel(customerSpecified);
                adminCustomer.setExistingCustomerPanelVisible(true);  
                adminCustomer.setLeftPanelVisible(false);
            }
            else
            {
                try
                {
                    CustomerSpecified customerSpecifiedFromKrak = model.handlers.CustomerHandler.doKrakSearchByNumber(number);
                    if (customerSpecifiedFromKrak != null)
                    {
                        adminCustomer.setAddressModel(customerSpecifiedFromKrak.getCustomerAddress());
                        adminCustomer.setCustomerModel(customerSpecifiedFromKrak.getCustomerName());
                        adminCustomer.setTelephoneModel(customerSpecifiedFromKrak.getCustomerTelephone());
                        adminCustomer.updateLeftPanel();
                        adminCustomer.setLeftPanelVisible(true);
                        adminCustomer.setExistingCustomerPanelVisible(false);
                        adminCustomer.updateNewCustomerPanel();
                    }
                    else
                    {
                        adminCustomer.clearCustomerAndAddressFields();
                        adminCustomer.setLeftPanelVisible(true);
                        adminCustomer.setExistingCustomerPanelVisible(false);
                        adminCustomer.updateNewCustomerPanel();
                    }
                } 
                catch (MalformedURLException ex)
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(true);
                    adminCustomer.updateNewCustomerPanel();
                } 
                catch (ParserConfigurationException ex)
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(true);
                    adminCustomer.updateNewCustomerPanel();
                } 
                catch (SAXException ex)
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(true);
                    adminCustomer.updateNewCustomerPanel();
                } 
                catch (IOException ex)
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(true);
                    adminCustomer.updateNewCustomerPanel();
                }
                catch (Exception ex)
                {
                    adminCustomer.clearCustomerAndAddressFields();
                    adminCustomer.setLeftPanelVisible(true);
                    adminCustomer.updateNewCustomerPanel();
                }
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }

    public JPanel getPanel()
    {
        return adminTelephonePanel;
    }
}